package com.rainner.admin.server;

import java.sql.SQLException;
import java.util.List;

import com.rainner.admin.javabean.RolePermissionBean;
import com.rainner.admin.util.dbutil.BaseService;

public class RolePermissionServer extends BaseService<RolePermissionBean>{
	/**
	 * 根据roleId返回所有的角色权限信息
	 * @param roleId
	 * @return
	 * @throws SQLException
	 */
	public List<RolePermissionBean> allRolePermissionData(String roleId) throws SQLException{
		String sqlSelect = "select * from RolePermission where roleId = ?";
		return super.query(sqlSelect,roleId);
	}
	
	/**
	 * 新增角色权限
	 * @param roleId
	 * @param permissionId
	 * @return
	 * @throws SQLException
	 */
	public boolean addRolePermission(String roleId,String permissionId) throws SQLException{
		String sqlSelect = "select COUNT(1) from RolePermission where roleId = ? and permissionId = ?";
		if(super.counts(sqlSelect, roleId,permissionId) < 1){
			String sqlInsert = "insert into RolePermission(RoleId,PermissionId) values(?,?)";
			return super.update(sqlInsert, roleId,permissionId);
		}
		return true;
	}
	
	/**
	 * 删除角色权限
	 * @param roleId
	 * @param permissionId
	 * @return
	 * @throws SQLException
	 */
	public boolean delRolePermission(String roleId,String permissionId) throws SQLException{
		String sqlSelect = "select COUNT(1) from RolePermission where roleId = ? and permissionId = ?";
		if(super.counts(sqlSelect, roleId,permissionId) > 0){
			String sqlDelete = "delete from RolePermission where RoleId = ? and PermissionId = ?";
			return super.update(sqlDelete, roleId,permissionId);
		}
		return true;
	}
	/**
	 * 判断用户是否具有相应权限
	 * @param permissionID 权限ID
	 * @param managerName 用户名
	 * @return true代表权限存在
	 * @throws SQLException
	 */
	public boolean checkPermission(String permissionID,String managerName) throws SQLException{
		String sqlSelect = "select COUNT(1)  from RolePermission where PermissionID = ? and RoleID in (select RoleID from ManagerRole where ManagerName = ?)";
		return (super.counts(sqlSelect, permissionID,managerName) > 0) ;
	}
}
